package com.ktwlsoft.archivemanage.controller.study;

import com.ktwlsoft.archivemanage.config.FileStorageProperties;
import com.ktwlsoft.archivemanage.domain.business.businessService.FileService;
import com.ktwlsoft.archivemanage.domain.business.businessService.study.StudyKnowledgeService;
import com.ktwlsoft.archivemanage.domain.dao.entity.study.StudyKnowledge;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.exception.BusinessException;

import com.ktwlsoft.archivemanage.payload.study.studyClass.MoveRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.util.List;

@Api(value = "知识库", description = "知识库")
@RestController
@RequestMapping("/study/knowledge")
public class StudyKnowledgeController {

    private final StudyKnowledgeService knowledgeService;

    public StudyKnowledgeController(StudyKnowledgeService knowledgeService, FileService fileService,
                                    FileStorageProperties fileStorageProperties) {
        this.knowledgeService = knowledgeService;
    }

    /**
     * 子系统知识库列表 on 2019/12/30 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "子系统知识库列表", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "list")
    public ResponseEntity<?> list(@RequestParam(defaultValue = "1") int pn,
                                  @RequestParam(defaultValue = "30") int pageSize,
                                  @ApiParam(name = "keyWords", value = "关键字") @RequestParam(defaultValue = "", required = false) String keyWords,
                                  @ApiParam(name = "parentId", value = "父级id") @RequestParam(defaultValue = "", required = false) Long parentId) {
        return ResponseEntity.ok(knowledgeService.list(pn, pageSize, keyWords, parentId, "USER"));
    }

    /**
     * 管理系统知识库列表 on 2019/12/30 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "管理系统知识库列表", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "manage/list")
    public ResponseEntity<?> manageList(@RequestParam(defaultValue = "1") int pn,
                                        @RequestParam(defaultValue = "30") int pageSize,
                                        @ApiParam(name = "keyWords", value = "关键字") @RequestParam(defaultValue = "", required = false) String keyWords,
                                        @ApiParam(name = "parentId", value = "父级id") @RequestParam(defaultValue = "", required = false) Long parentId) {
        return ResponseEntity.ok(knowledgeService.list(pn, pageSize, keyWords, parentId, "MANAGE"));
    }

    /**
     * 文件预览 on 2019/12/30 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "文件预览", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "view")
    public ResponseEntity<?> view(@ApiParam(name = "fileName", value = "文件名") @RequestParam String fileName, HttpServletRequest request) {
        return knowledgeService.view(fileName, request);
    }

    /**
     * 删除文件 on 2019/12/30 12:14
     *
     * @param fileName fileName
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "删除文件", httpMethod = "DELETE", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @DeleteMapping("delete")
    public ResponseEntity<?> delete(@ApiParam(name = "fileName", value = "文件名") @RequestParam String fileName) {
        return ResponseEntity.ok(knowledgeService.delete(fileName));
    }

    /**
     * 文件移动 on 2019/12/30 12:14
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "文件移动", httpMethod = "PUT", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PutMapping("move")
    public ResponseEntity<?> move(@RequestBody MoveRequest request) {
        return ResponseEntity.ok(knowledgeService.move(Long.parseLong(request.getParentId()),request.getFiles()));
    }

    /**
     * 文件下载 on 2019/12/30 15:40
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "文件下载", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "download")
    public ResponseEntity<?> download(@ApiParam(name = "fileName", value = "文件名") @RequestParam String fileName, HttpServletRequest request) {
        return knowledgeService.download(fileName, request);
    }

    @ApiOperation(value = "管理端文件上传")
    @PostMapping("/managerUpload")
    public ResponseEntity<?> managerUpload(@RequestParam("file") MultipartFile file,
                                           @RequestParam("fileName") String fileName, String originFileName, String targetDir, int sliceNum, boolean isLast, Long parentId) {
        if (!targetDir.startsWith("/")) {
            throw new BusinessException("targetPath必须以'/'开头");
        }
        Result<?> result = knowledgeService.checkFileType(file);
        if(result.getResultType().equals(ResultType.CHECK_ERROR)){
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.ok(Result.ok(
                knowledgeService.manageFileUpload(file, fileName, targetDir, sliceNum, isLast, parentId, "MANAGE", originFileName)));
    }

    @ApiOperation(value = "用户端文件上传")
    @PostMapping("/userUpload")
    public ResponseEntity<?> userUpload(@RequestParam("file") MultipartFile file,
                                        @RequestParam("fileName") String fileName, String originFileName, String targetDir, int sliceNum, boolean isLast, Long parentId) {
        if (!targetDir.startsWith("/")) {
            throw new BusinessException("targetPath必须以'/'开头");
        }
        Result<?> result = knowledgeService.checkFileType(file);
        if(result.getResultType().equals(ResultType.CHECK_ERROR)){
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.ok(Result
                .ok(knowledgeService.manageFileUpload(file, fileName, targetDir, sliceNum, isLast, parentId, "USER", originFileName)));
    }

    @ApiOperation(value = "用户端文件上传(非分片上传)")
    @PostMapping("/userUploadFolder")
    @UserAuthenticate(actionCode = "", skip = true)
    public ResponseEntity<?> userUploadFolder(@RequestParam MultipartFile file, @RequestParam String targetDir, @RequestParam Long parentId) throws IOException {
        if (!targetDir.startsWith("/")) {
            throw new BusinessException("targetPath必须以'/'开头");
        }
        Result<?> result = knowledgeService.checkFileType(file);
        if(result.getResultType().equals(ResultType.CHECK_ERROR)){
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.ok(knowledgeService.uploadFolder(file, targetDir, parentId, "USER"));
    }

    @ApiOperation(value = "管理端文件上传(非分片上传)")
    @PostMapping("/manageUploadFolder")
    @UserAuthenticate(actionCode = "", skip = true)
    public ResponseEntity<?> manageUploadFolder(@RequestParam MultipartFile file, @RequestParam String targetDir, @RequestParam Long parentId) throws IOException {
        if (!targetDir.startsWith("/")) {
            throw new BusinessException("targetPath必须以'/'开头");
        }
        Result<?> result = knowledgeService.checkFileType(file);
        if(result.getResultType().equals(ResultType.CHECK_ERROR)){
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.ok(knowledgeService.uploadFolder(file, targetDir, parentId, "MANAGE"));
    }

    @ApiOperation(value = "管理端创建文件夹")
    @GetMapping("/manage/createFolder")
    public ResponseEntity<?> manageCreateFolder(@RequestParam long parentId, String folderName) throws IOException {
        if (folderName.startsWith("/")) {
            throw new BusinessException("folderName不能以'/'开头");
        }
        return ResponseEntity.ok(Result.ok(knowledgeService.createFolder(parentId, "MANAGE", folderName)));
    }

    @ApiOperation(value = "用户端创建文件夹")
    @GetMapping("/user/createFolder")
    public ResponseEntity<?> userCreateFolder(@RequestParam long parentId, String folderName) throws IOException {
        if (folderName.startsWith("/")) {
            throw new BusinessException("folderName不能以'/'开头");
        }
        return ResponseEntity.ok(Result.ok(knowledgeService.createFolder(parentId, "USER", folderName)));
    }
}
